package com.qyhl.webtv.module_user.share;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.flyco.tablayout.SlidingTabLayout;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.ContextUtilts;
import com.qyhl.webtv.basiclib.utils.glide.GlideCircleTransform;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.utils.config.HomeThemeColorUtil;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_user.R;
import com.qyhl.webtv.module_user.R2;
import com.qyhl.webtv.module_user.util.ViewPagerForNS;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.SHARE_HOME ,extras = ARouterPathConstant.LOGIN_NEEDED)
public class ShareHome extends BaseActivity {
    @BindView(R2.id.tablayout)
    SlidingTabLayout tablayout;
    @BindView(R2.id.userImg)
    ImageView userImg;
    @BindView(R2.id.viewPager)
    ViewPagerForNS viewPager;
    @BindView(R2.id.share_code)
    TextView share_code;

    private UserShareUtil shareUtil;
    private BottomSheetDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.login_activity_share_home;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        initview();
    }

    @Override
    protected void setListener() {

    }


    private void initview() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(TeachFragment.newInstance(""));
        fragmentList.add(FriendListFragment.newInstance());
        viewPager.setAdapter(new ShareVPAdapter(getSupportFragmentManager(), fragmentList));
        tablayout.setViewPager(viewPager);
        tablayout.setTextSelectColor(ContextCompat.getColor(ContextUtilts.getInstance().getmContext(), R.color.global_base_0));
        tablayout.setTextUnselectColor(HomeThemeColorUtil.getTabNormalColor());
        tablayout.getTitleView(0).getPaint().setFakeBoldText(true);
        String headicon = CommonUtils.getInstance().getUserLogo();
        Glide.with(this)
                .load(headicon)
                .apply(new RequestOptions()
                        .centerCrop()
                        .error(R.drawable.comment_head_default)
                        .transform(new GlideCircleTransform(this)))
                .into(userImg);

        String code = CommonUtils.getInstance().getCode();
        share_code.setText(code);

        shareUtil = new UserShareUtil(this, 50, null);
    }


    @OnClick({R2.id.back, R2.id.share_circle, R2.id.share_wechat, R2.id.nowInvitation, R2.id.share_qq, R2.id.share_message, R2.id.share_code_copy, R2.id.share_facinttow})
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.back) {
            finish();

        } else if (i == R.id.nowInvitation) {
            showBottomDialog();

        } else if (i == R.id.share_circle) {
            shareUtil.share("Circle");

        } else if (i == R.id.share_wechat) {
            shareUtil.share("WeiXin");

        } else if (i == R.id.share_qq) {
            shareUtil.share("QQ");

        } else if (i == R.id.share_message) {
            shareUtil.sendSMS();

        } else if (i == R.id.share_code_copy) {
            shareUtil.clipBoard();

        } else if (i == R.id.share_facinttow) {
            RouterManager.router(ARouterPathConstant.SHARE_FACINGTWO);

        } else {
        }
    }

    private void showBottomDialog() {
        if (dialog == null) {
            dialog = new BottomSheetDialog(this);
            //设置dialog的view
            View inflate = LayoutInflater.from(this).inflate(R.layout.login_share_home_dialoglayout, null);

            View.OnClickListener onClickListener = v -> {
                int i = v.getId();
                if (i == R.id.share_friend_cicle) {
                    shareUtil.share("Circle");

                } else if (i == R.id.share_wechat) {
                    shareUtil.share("WeiXin");

                } else if (i == R.id.share_qq) {
                    shareUtil.share("QQ");

                } else if (i == R.id.share_message) {
                    shareUtil.sendSMS();

                } else if (i == R.id.share_QZ) {
                    shareUtil.share("QZ");

                } else if (i == R.id.share_facingtwo) {
                    RouterManager.router(ARouterPathConstant.SHARE_FACINGTWO);

                } else {
                }
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            };

            inflate.findViewById(R.id.share_friend_cicle).setOnClickListener(onClickListener);
            inflate.findViewById(R.id.share_wechat).setOnClickListener(onClickListener);
            inflate.findViewById(R.id.share_facingtwo).setOnClickListener(onClickListener);
            inflate.findViewById(R.id.share_message).setOnClickListener(onClickListener);
            inflate.findViewById(R.id.share_QZ).setOnClickListener(onClickListener);
            inflate.findViewById(R.id.share_qq).setOnClickListener(onClickListener);
            inflate.findViewById(R.id.cancle).setOnClickListener(onClickListener);
            dialog.setContentView(inflate);
        }
        dialog.show();
    }

    @Override
    protected void onDestroy() {
        shareUtil.onDestroy();
        super.onDestroy();
    }


}
